#ifndef __CONNECTOR_H__
#define __CONNECTOR_H__

#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
#include <vector>
#include "Connection.hpp"

/// Downloads stock quote information from a server.
class CConnector
{
public:
			CConnector( boost::asio::io_service& io_service, const std::string& host, const std::string& service );
	virtual ~CConnector( void );

public:
	void Connect( void );

	template< typename T > void Read( T& t )
	{
		m_Connection.async_read( t, boost::bind(&CConnector::OnRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred) );
	}

	template< typename T > void Write( T& t )
	{
		m_Connection.async_write( t, boost::bind(&CConnector::OnWrite, this, boost::asio::placeholders::error) );
	}

protected:
	// Handle completion of a connect operation.
	void OnConnect( const boost::system::error_code& e );
	
	// Handle completion of a read operation.
	void OnRead( const boost::system::error_code& e, size_t bytes_transferred );

	// Handle completion of a write operation.
	void OnWrite( const boost::system::error_code& e );

private:
	virtual void OnConnectFailed( const boost::system::error_code& e )							= 0;
	virtual void OnReadFailed( const boost::system::error_code& e, size_t bytes_transferred )	= 0;
	virtual void OnWriteFailed( const boost::system::error_code& e )							= 0;
	virtual void OnConnectSuccessed( void )														= 0;
	virtual void OnReadSuccessed( void )														= 0;
	virtual void OnWriteSuccessed( void )														= 0;

protected:
	boost::asio::ip::tcp::resolver::iterator	endpoint_iterator;
	CConnection									m_Connection;	// The connection to the server.
};

#endif	// __CONNECTOR_H__